package com.yer.note.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;

import com.yer.note.R;
import com.yer.note.adapter.DiaryAdapter;
import com.yer.note.entity.Diary;
import com.yer.note.entity.User;
import com.yer.note.utils.BaseUtils;

import java.util.ArrayList;
import java.util.List;

public class nav_label_diary extends AppCompatActivity {
    private ArrayList<Diary> diaries = new ArrayList<>();
    private RecyclerView recyclerview;
    private DiaryAdapter diaryAdapter;
    private String label;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav_label_diary);
        Intent intent = getIntent();
        label = intent.getStringExtra("label");
        initData();
        initRecyclerview();
    }
    private void initData() {
        List<Diary> list = Diary.listAll(Diary.class);
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUseruuid().equals(User.user.getMyuuid())){
                if(list.get(i).getLabel().equals(label)){
                    diaries.add(list.get(i));
                }
            }
        }
    }

    private void initRecyclerview() {
        //获得Recyclerview
        recyclerview = (RecyclerView)findViewById(R.id.recyclerview_label_diary);
        //创建adapter类的对象
        diaryAdapter = new DiaryAdapter(nav_label_diary.this, diaries);
        diaryAdapter.setOnItemClickLitener(new DiaryAdapter.OnItemClick() {
            @Override
            public void onItemLongClick(String diaryUUid) {
                String news = "正在跳转到日记编辑页面";
                BaseUtils.shortTipInCoast(nav_label_diary.this,news);
                Intent intent = new Intent(nav_label_diary.this, KeepDiaryActivity.class);
                intent.putExtra("diaryUUid",diaryUUid);
                startActivity(intent);
            }
        });
        //将对象作为参数通过setAdapter方法设置给recylerview；
        recyclerview.setAdapter(diaryAdapter);
        //这步骤必须有，这是选择RecylerView的显示方式
        recyclerview.setLayoutManager(new LinearLayoutManager(nav_label_diary.this));
    }
}